var dishList = [];
function getCart() {
	var cartMap2 = new Map();
	var url = "ShowCartServlet";
	var httpReq = new XMLHttpRequest();
	httpReq.onreadystatechange = function() {
		if (httpReq.readyState == 4 && httpReq.status == 200) {
			var result = httpReq.responseText;
			var list = JSON.parse(result);
			dishList = [];
			for (food in list) {
				console.log(list[food])
				
				var dish = new Dish(list[food].dId, list[food].dName,
						list[food].dPic, list[food].dPrice, list[food].num,
						list[food].cartId, list[food].cdId);
				dishList.push(dish);

			}
			if (dishList.length > 0) {
				showCart();
			}
		}
	}
	httpReq.open("POST", url, true);
	httpReq.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	httpReq.send();
}

window.onload = function() {
	getCart();

	for (var i = 0; i < 12; i++) {
		if (document.getElementById("buy" + i)) {
			document.getElementById("buy" + i).onclick = buy1;
			var cId = document.getElementById("cId").innerHTML;
		}

		// alert(cId);
	}

}
console.log(dishList);
var cartMap = new Map();
// for(var i=0;i<listDish.length;i++){
//	
// }
function buy1() {
	var dId = this.parentNode.parentNode.getElementsByClassName("dId")[0].innerHTML;
	var cId = document.getElementById("cId").innerHTML;
	var flag = 1;
	for (var i = 0; i < dishList.length; i++) {
		if (dishList[i].dId == dId) {
			dishList[i].dNum += 1;
			var num = dishList[i].dNum;
			var cdId = dishList[i].cdId;
			var url = "UpdateCartServlet";
			var httpReq = new XMLHttpRequest();
			httpReq.onreadystatechange = function() {
				if (httpReq.readyState == 4 && httpReq.status == 200) {
					var result = httpReq.responseText;
					getCart();
				}
			}

			httpReq.open("POST", url, true);
			httpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
			httpReq.send("cdId=" + cdId + "&dNum=" + num);
			flag = 0;
		}
	}
	if (flag == 1) {
		var url = "AddCartServlet";
		var httpReq = new XMLHttpRequest();
		httpReq.onreadystatechange = function() {
			if (httpReq.readyState == 4 && httpReq.status == 200) {
				var result = httpReq.responseText;
				dishList = [];
				getCart();
			}
		}
		httpReq.open("POST", url, true);
		httpReq.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded")
		httpReq.send("dId=" + dId + "&cId=" + cId);

	}

}
// 定义一个Cartmap

function Dish(dId, dName, dPic, dPrice, dNum, cartId, cdId) {
	this.dId = dId;
	this.dName = dName;
	this.dPic = dPic;
	this.dPrice = dPrice;
	this.dNum = dNum;
	this.cartId = cartId;
	this.cdId = cdId;
}

var DishPrototype = {
	constructor : Dish,
	addDish : function() {
		this.dNum++;
	},
	cutDish : function() {
		this.dNum--;
	}
}
Dish.prototype = DishPrototype;
// 1.把一个List<Dish>对象存入一个map里面并显示出来
// List<Dish> listDish = new ArrayList<Dish>();
// for(Dish d:listDish){
// cartMap.put(d.dId,listDish);
// }
// 遍历map，把数据存入map
// cartMap.forEach(function(item,index,array){
//	
// })
// 2.每次点击buy就匹配是否有，有就update没有就add
// for(){
//	
// }
// 3.点击num进行update操作，并判断为0时删除该行
// 4.购物车上点击购买时跳转到订单页面，只需传一个List<Dish>过去

var dishListArr = [];
function addDish() {
	for (var i = 0; i < dishList.length; i++) {

	}
}
function showCart() {

	if (document.getElementById("cart_table")) {
		var cart_table = document.getElementById("cart_table");
		delTable(cart_table);
		var thead = document.createElement("tr");

		thead.innerHTML = "<tr><th>序号</th><th>图片</th><th>商品名</th><th>价格</th><th>数量</th><th>小计</th></tr>"
		cart_table.appendChild(thead);
		var sumPrice = 0;
		for (var i = 0; i < dishList.length; i++) {
			var contr = document.createElement("tr");
			contr.innerHTML = ("<tr><td>" + (i + 1) + "</td><td><img src='"
					+ dishList[i].dPic + "' class='img-pic'></td><td>"
					+ dishList[i].dName + "</td><td>￥" + dishList[i].dPrice
					+ "</td><td><button id='cut" + i
					+ "'>-</button><p style='display:none'>"+dishList[i].dId +"</p><span class='dNum'>" + dishList[i].dNum
					+ "</span><button id=add" + i
					+ ">+</button></td><td>" + dishList[i].dNum
					* dishList[i].dPrice + "</td></tr>");
			sumPrice = sumPrice + dishList[i].dNum * dishList[i].dPrice;
			cart_table.appendChild(contr);
		}
		var sumPriceP = document.getElementById("sumPrice");
		var sumP = document.createElement("span");
		sumP.innerHTML = sumPrice;
		sumPriceP.appendChild(sumP);
		var btn = document.createElement("button");
		btn.setAttribute("class", "account-btn");
		btn.innerHTML = "Buy All";
		sumP.parentNode.parentNode.appendChild(btn);
		btn.onclick = order;
		for (var i = 0; i < dishList.length; i++) {
			document.getElementById("add" + i).onclick = addNum;
			document.getElementById("cut" + i).onclick = cutNum;

		}
	}
	if (document.getElementById("order_table")) {
		var cart_table = document.getElementById("order_table");
		delTable(cart_table);
		var thead = document.createElement("tr");

		thead.innerHTML = "<tr><th>序号</th><th>图片</th><th>商品名</th><th>价格</th><th>数量</th><th>小计</th></tr>"
		cart_table.appendChild(thead);
		var sumPrice = 0;
		for (var i = 0; i < dishList.length; i++) {
			var contr = document.createElement("tr");
			contr.innerHTML = ("<tr><td>" + (i + 1) + "</td><td><img src='"
					+ dishList[i].dPic + "' class='img-pic'></td><td>"
					+ dishList[i].dName + "</td><td>￥" + dishList[i].dPrice
					+ "</td><td>" + dishList[i].dNum + "</td><td>"
					+ dishList[i].dNum * dishList[i].dPrice + "</td></tr>");
			sumPrice = sumPrice + dishList[i].dNum * dishList[i].dPrice;
			cart_table.appendChild(contr);
				
			
		}
		
	}
	if (document.getElementById("orderSum")) {
		//order.jsp
		var sumPriceP = document.getElementById("orderSum");
		var sumP = document.createElement("span");
		sumP.innerHTML = sumPrice;
		sumPriceP.appendChild(sumP);
		var btn = document.createElement("button");
		btn.setAttribute("class", "order-btn");
		btn.innerHTML = "Account";
		sumP.parentNode.parentNode.appendChild(btn);
		btn.onclick = orderHistory;
	}
}
// 删除table里面的所有行http://www.cnblogs.com/cuihongyu3503319/archive/2007/02/12/648174.html
function delTable(cart_table) {
	var rowNum = cart_table.rows.length;
	for (var i = 0; i < rowNum; i++) {
		cart_table.deleteRow(i);
		rowNum = rowNum - 1;
		i = i - 1;
	}
}
//addNum()
function addNum() {
//	for (var i = 0; i < dishList.length; i++) {
//
//	}
	var dId = this.parentNode.getElementsByTagName("p")[0].innerHTML;
	alert(dId);
	
	//alert(this.parentNode.getElementsByClassName("dNum")[0].innerHTML);
	
}

//cutNum()
function cutNum() {
	alert("13");
}
//
function order() {
	window.location.href = "order.jsp";
}
function orderHistory() {
	window.location.href = "ShowOrderServlet";
}
